﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 删除文件空行
{
    public partial class Frm_CleanEmptyLine : Form
    {
        public Frm_CleanEmptyLine()
        {
            InitializeComponent();
        }
        delegate void SetLogCallback(string imgType, string logInfo);
        private void WriteLog(string imgType, string logInfo)
        {
            ListViewItem infoMsg = new ListViewItem();
            infoMsg.ImageKey = imgType;
            infoMsg.Text = logInfo;
            infoMsg.ToolTipText = logInfo;
            infoMsg.SubItems.Add(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            if (hisView.InvokeRequired)
            {
                SetLogCallback logCall = new SetLogCallback(WriteLog);
                this.Invoke(logCall, new object[] { imgType, logInfo });
            }
            else
            {
                hisView.Items.Add(infoMsg);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = this.txtOutPut.Text;
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                txtOutPut.Text = dialog.SelectedPath;
                WriteLog(ImgConst.SUCC, "初始化系统配置成功！");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.txtOutPut.Text.Length == 0) MessageBox.Show("请选择指定路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            if (this.txtfiletype.Text.Length == 0) MessageBox.Show("请指定文件后缀名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            string path = this.txtOutPut.Text;
            FindFile(path);
            this.progressBar1.Maximum = fileList.Count;
            this.progressBar1.Minimum = 0;
            for (int i = 0; i < fileList.Count; i++)
            {
                #region 删除代码中的所有空行
                string inptuFileName = fileList[i];
                string tempFileName = Path.GetTempFileName();
                try
                {
                    using (var streamReader = new StreamReader(inptuFileName))
                    using (var streamWriter = new StreamWriter(tempFileName))
                    {
                        string line;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                streamWriter.WriteLine(line);
                            }
                        }
                    }
                    File.Copy(tempFileName, inptuFileName, true);
                }
                catch (Exception ex)
                {
                    WriteLog(ImgConst.ERROR, Path.GetFileName(inptuFileName) + ex.Message);
                }
                finally
                {
                    File.Delete(tempFileName);
                }
                #endregion
                WriteLog(ImgConst.SUCC, Path.GetFileName(inptuFileName));
                this.progressBar1.Value = i;
            }
            this.progressBar1.Value = 0;
            WriteLog(ImgConst.INFO, " 删除代码中的所有空行完成！");
        }
        List<string> fileList = new List<string>();
        List<string> notDir = new List<string>();
        public void FindFile(string dir)
        {
            DirectoryInfo Dir = new DirectoryInfo(dir);
            foreach (DirectoryInfo d in Dir.GetDirectories())
            {
                if (!notDir.Contains(d.Name))
                {
                    FindFile(d.FullName);
                }
            }
            foreach (FileInfo f in Dir.GetFiles(this.txtfiletype.Text))
            {
                fileList.Add(f.FullName);
            }
        }
    }
    public class ImgConst
    {
        public const string INFO = "info";
        public const string SUCC = "succ";
        public const string WARN = "warn";
        public const string ERROR = "error";
        public ImgConst()
        {
        }
    }
}
